public class ReplacingWithGroups {

	public static void main(String[] args) {
		// Exchange the first 2 words in a sentence
		String sentence = "Hello, Java Enterprise!"; 
		String regex = "(\\w+)(\\W+)(\\w+)";
		String newSentence = sentence.replaceAll(regex, "$3$2$1");
		System.out.println(newSentence); // Java, Hello Enterprise!
		System.out.println();
		
		// Convert the URL to another format 
		String text = 
			"NASD: [url]http://academy.devbg.org[/url]\n" +
			"BASD: [url]http://www.devbg.org[/url]\n" +
			"Java: [url]http://java.sun.com[/url]\n";
		String urlRegex = "\\[url\\](.*)\\[/url\\]";
		String newText = text.replaceAll(urlRegex, "<a href=\"$1\">$1</a>");
		System.out.println(newText);		
	}
	
}
